package com.smarttourism.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttourism.common.core.domain.R;
import com.smarttourism.domain.Customer;
import com.smarttourism.service.CustomerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CutsomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('services:customer:list')")
    public R getCustomerList(@Param("pageNum") int pageNum,@Param("pageSize") int pageSize, @Param("name") String name){
        PageHelper.startPage(pageNum,pageSize);
        List<Customer> customers = customerService.findCustomers(name);
        PageInfo<Customer> customerPageInfo = new PageInfo<>(customers);
        if (customers !=null){
            return R.ok(customerPageInfo);
        }else return R.fail("查询失败");
    }

    @PostMapping("/add")
    public R addCustomer(@RequestBody Customer customer){
        int i = customerService.insertCustomer(customer);
        if (i>0){
            return R.ok("新增成功");
        }else {
            return R.fail("新增失败");
        }
    }
    @PutMapping("/deletes")
    public R deleteCustomers(@Param("ids") Integer[] ids){
        int i = customerService.deleteCustomers(ids);
        if (i>0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping("/delete")
    public R deleteCustomer(@Param("id") int id){
        int i = customerService.deleteCustomer(id);
        if (i>0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping("/")
    public R updateCustomer(@RequestBody Customer customer){
        int i = customerService.updateCustomer(customer);
        if (i>0){
            return R.ok("更新成功");
        }else {
            return R.fail("更新失败");
        }
    }


}
